package cn.lysj.framework.order.controller;

import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.ConditionUtil;
import cn.lysj.framework.common.utils.pagination.PageUtil;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.random.CustomTimeType;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.order.domain.FoodOrder;
import cn.lysj.framework.order.repository.FoodOrderRepository;
import cn.lysj.framework.pirpraph.service.PirPraphService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 11:26 2019/1/2
 * @ Description:
 **/

@RestController
@RequestMapping("/day_sum_order")
public class DaySumOrderController {

    @Resource
    FoodOrderRepository foodOrderRepository;

    @GetMapping("/list")
    public Resp list(@CurrentUser User user, FoodOrder foodOrder, PageVo pageVo) {

        List<Object[]> list = foodOrderRepository.findByDayDesc(foodOrder.getStart_createTime(),
                CustomTimeUtil.getSkewingTimeInDay(foodOrder.getEnd_createTime(),0, CustomTimeType.END), user.getStoreId());
        Map<String, Object> respMap = new HashMap();
        int count = list.size();
        if (ParamUtil.isBlank(pageVo.getPageSize())) {
             pageVo.setPageSize(10);
        }
        if (ParamUtil.isBlank(pageVo.getPageNumber())) {
            pageVo.setPageNumber(0);
        }
        Integer limit = pageVo.getPageSize();
        Integer page = pageVo.getPageNumber();
        respMap.put("totalPage", count % pageVo.getPageSize() == 0 ? count / limit : count / limit + 1);
        //数据总数小于每页显示的数据条数
        if (page == 0) {
            if (count <= limit)  //数据总数小于每页显示的数据条数
            {
                //截止到总的数据条数(当前数据不足一页，按一页显示)，这样才不会出现数组越界异常
                respMap.put("rows", list.subList(0, count));
            } else {
                respMap.put("rows", list.subList(0, limit));
            }
        }
        //截取起始下标
        int fromIndex = page * limit;
        //截取截止下标
        int toIndex = page * limit + limit;
        //若当前请求条数小于等于总条数
        if (fromIndex <= count) {
            if (count < toIndex) {
                respMap.put("rows", list.subList(fromIndex, count));
            } else {
                respMap.put("rows", list.subList(page * limit, page * limit + limit));
            }
        } else {
            respMap.put("rows", "");
        }
        return new Resp(respMap);
    }
}
